﻿using MvcApplication.Common;
using MvcApplication.Entity;
using MvcApplication.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication.Controllers
{
    public class MemberController : Controller
    {
        IMemberService _memberService;
        public MemberController(IMemberService memberService)
        {
            _memberService = memberService;
        }

        [Permission(Entity.Menu.用户管理, Entity.PermissionType.访问)]
        public ActionResult List(UserInfoFilter filter, Pager pager)
        {
            var userInfoes = _memberService.GetUsers(filter, pager);
            return View(userInfoes);
        }

        [Permission(Entity.Menu.用户管理, Entity.PermissionType.编辑)]
        public JsonResult NewUserInfo(UserInfo userInfo)
        {
            var result = _memberService.NewUserInfo(userInfo);
            return Json(result);
        }

        [Permission(Entity.Menu.用户管理, Entity.PermissionType.编辑)]
        public ActionResult Edit(long uid)
        {
            var user = _memberService.GetUser(uid);
            if (user == null) return HttpNotFound();

            return View(user);
        }

        [HttpPost]
        [Permission(Entity.Menu.用户管理, Entity.PermissionType.编辑)]
        public JsonResult EditUserInfo(UserInfo userInfo)
        {
            var user = _memberService.EditUserInfo(userInfo);
            return Json(user);
        }

        [HttpPost]
        [Permission(Entity.Menu.用户管理, Entity.PermissionType.编辑)]
        public JsonResult EditUserStatus(List<long> uids, UserStatus status)
        {
            _memberService.EditStatus(uids, status);
            return Json(true);
        }
    }
}